package user;

import book.Book;
import book.Library;
import utils.ScannerSingleton;

import java.time.LocalDate;
import java.util.Scanner;

public class AdminUser extends User{
    public Scanner scanner;
    public Library library;
    public AdminUser(String name, int userId) {
        super(name, userId, "管理员");
        scanner = ScannerSingleton.getScannerSingleton();
        library = Library.getLibrary();
    }


    @Override
    public int display() {
        System.out.println("管理员 " + this.getName() + " 的操作菜单:");
        System.out.println("1. 查找图书");
        System.out.println("2. 打印所有的图书");
        System.out.println("3. 退出系统");
        System.out.println("4. 上架图书");
        System.out.println("5. 修改图书");
        System.out.println("6. 下架图书");
        System.out.println("7. 统计借阅次数");
        System.out.println("8. 查看最受欢迎的前K本书");
        System.out.println("9. 查看库存状态");
        System.out.println("10. 按类别统计图书 ");
        System.out.println("11. 按作者统计图书 ");
        System.out.println("12. 检查超过⼀年未下架的图书");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }
    //上架图书
    public void addBook(){
        System.out.println("AdminUser的addBook执行了");
        scanner.nextLine();

        System.out.println("请输入书名");
        String title = scanner.nextLine(); // 输⼊书名
        System.out.println("请输⼊作者：");
        String author = scanner.nextLine(); // 输⼊作者
        System.out.println("请输⼊类别：");
        String category = scanner.nextLine(); // 输⼊图书类别
        System.out.println("请输⼊出版年份：");
        int year = scanner.nextInt(); // 输⼊出版年份
        scanner.nextLine(); // 吞掉换⾏符
        // 当前时间作为上架时间
        LocalDate shelfDate = LocalDate.now();
        // 创建新书对象
        Book newBook = new Book(title, author, category, year, shelfDate);
        //调⽤图书类 添加图书
        library.addBook(newBook);
    }
    //修改图书
    public void updateBook(){
        library.displayBook();
        System.out.println("请输入要修改的图书ID");
        int bookId = scanner.nextInt();

        scanner.nextLine();

        Book book = library.searchById(bookId);

        if(book == null){
            System.out.println("没有你要找的为此ID这本书"+bookId );
            return;
        }
        System.out.println("当前书名：" + book.getTitle());
        System.out.println("请输⼊新的书名：");
        String newTitle = scanner.nextLine(); // 输⼊新的书名
        System.out.println("当前作者：" + book.getAuthor());
        System.out.println("请输⼊新的作者：");
        String newAuthor = scanner.nextLine(); // 输⼊新的作者
        System.out.println("当前类别：" + book.getCategory());
        System.out.println("请输⼊新的类别：");
        String newCategory = scanner.nextLine(); // 输⼊新的类别

        //更新对应书籍的信息
        book.setTitle(newTitle);
        book.setAuthor(newAuthor);
        book.setCategory(newCategory);
        library.updateBook(book);


    }
    //删除图书

    //统计图书借阅次数
    public void borrowCount(){
        System.out.println("Admin的borrowBook执行了");
        library.borrowCount();

    }
    //查询最受欢迎的前n本书
     public void generateBook(){

         System.out.println("请输⼊你要查看的最受欢迎的前K本书,注意k值不能超过："+library.bookCount);

         int k = scanner.nextInt();
         if(k <= 0 || k > library.bookCount) {
             System.out.println("没有最受欢迎的前"+k+"本书！");
             return;
         }
         library.generateBook(k);

     }

     //查看图书库存情况
     public void checkInventoryStatus(){
        library.checkInventoryStatus();

     }
     //按照类别统计图书
    public void categorizeBooksByCategory(){
        library.categorizeBooksByCategory();

    }
    //按照作者统计图书
    public void categorizeBooksByAuthor(){
        library.categorizeBooksByAuthor();

    }
    //移除超过一年的书
    public void checkAndRemoveOldBooks(){
        library.checkAndRemoveOldBooks();

    }
    public void exit(){
        System.exit(0);
    }


    public void removeBook() {
        library.displayBook();
        System.out.println("请输入要下架的图书ID");
        int bookId = scanner.nextInt();

        scanner.nextLine();

        Book removeBook = library.searchById(bookId);

        if(removeBook == null){
            System.out.println("没有你要找的为此ID这本书"+bookId );
            return;
        }

        library.removeBook(bookId);

        System.out.println("书籍已删除");

    }
}
